package com.wjggy.core.action.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.common.page.Page;
import com.wjggy.core.common.page.PageUtil;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.entities.User;
import com.wjggy.core.service.user.ManageUserService;

@Controller
@RequestMapping("/admin/user")
public class UserAction {
	@Autowired
	private ManageUserService manageUserService;
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView userList(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView model = ViewUtil.findView("admin/user/userList");
		Page page = PageUtil.createPage(request, "", "");
		page.setResult(manageUserService.getUsers(page));
		model.addObject("page", page);
		return model;
	}
	@RequestMapping(value="add",method=RequestMethod.GET)
	public ModelAndView userAdd(){
		return ViewUtil.findView("admin/user/addUser");
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ModelAndView userAdded(User user,HttpServletRequest request,
			HttpServletResponse response){
		manageUserService.saveUser(user);
		return userList(request,response);
	}
	
	@RequestMapping(value="edit/{id}",method=RequestMethod.GET)
	public ModelAndView userEdit(@PathVariable Long id){
		User user=manageUserService.getUserById(id);
		ModelAndView model=ViewUtil.findView("admin/user/editUser");
		model.addObject("user", user);
		return model;
	}
	@RequestMapping(value="edit",method=RequestMethod.POST)
	public ModelAndView userEdited(User user,HttpServletRequest request,
			HttpServletResponse response){
		manageUserService.saveUser(user);
		return userList(request,response);
	}
	@RequestMapping(value="delete/{id}",method=RequestMethod.GET)
	public ModelAndView userDelete(@PathVariable Long id,HttpServletRequest request,
			HttpServletResponse response){
		manageUserService.deleteUser(id);
		return userList(request,response);
	}
}
